Python 字典练习

        做一个小练习,当我们在执行脚本的时候,让我们从键盘上读取输入,然后把这些输入存到字典了,再把这个字典打印出来:

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# vim 2.py
#!/usr/bin/python
info = {}
name = raw_input("Please input name:")
age = raw_input("Please input age:")
gender = raw_input('Please input (M/F):')
info['name'] = name
info['age'] = age
info['gender'] = gender
print info

        也可以写成

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/python
info = {}
name = raw_input("Please input name:")
age = raw_input("Please input age:")
gender = raw_input('Please input (M/F):')
info['name'] = name
info['age'] = age
info['gender'] = gender
for k,v in info.items():
print "%s: %s" % (k,v)
print 'hello'